package com.hll.util;

import com.hll.exception.DBException;

import java.sql.*;

public class DBUtil {

    public static Connection getConnection(){

        //1，加载驱动
        try {
            Class.forName(PropertyUtil.getProperty("jdbc.driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String URL = PropertyUtil.getProperty("jdbc.url");

        //2，链接connection对象
        Connection con  = null;
        try {
            con = DriverManager.getConnection(URL,PropertyUtil.getProperty("jdbc.username"),PropertyUtil.getProperty("jdbc.password"));
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException();
        }
        return con;
    }

    //释放资源
    public static void close(ResultSet rs, PreparedStatement stmt,Connection con) {
        try {
            if(rs != null){
                rs.close();
            }
            if(stmt != null){
                stmt.close();
            }
            if(con != null){
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}